package net.zjitc.service.impl;

import net.zjitc.mapper.CategoryMapper;
import net.zjitc.mapper.ProductMapper;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.service.ProductService;
import net.zjitc.vo.ProductVo;
import net.zjitc.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author admin
 * @DATE 2019/3/29 12:30
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    @Override
    public Product findById(String id) {
        return productMapper.findById(id);
    }

    @Override
    public int addProduct(Product product) {
        return productMapper.addProduct(product);
    }

    @Override
    public int editProduct(Product product) {
        return productMapper.editProduct(product);
    }

    @Override
    public int deleteProduct(String id) {
        return productMapper.deleteProduct(id);
    }


    @Override
    public List<Product> searchByCondition(ProductVo vo) {
        List<Product> list = productMapper.searchByCondition(vo);
        for (Product product :list){
            Category category = categoryMapper.findById(product.getCid());
            product.setCategory(category);
        }
        return list;
    }

    @Override
    public List<Product> findBycid(Integer id) {
        if (id==null){
            return productMapper.findAll();
        }
        return productMapper.findBycid(id);
    }

    @Override
    public List<Product> searchProduct(SearchVo vo) {
        return productMapper.searchProduct(vo);
    }

    @Override
    public List<Product> search(ProductVo productVo) {
        List<Product> list = productMapper.search(productVo);
        for (Product product :list){
            Category category = categoryMapper.findById(product.getCid());
            product.setCategory(category);
        }
        return list;
    }

}
